import logging
from logging import handlers
import config


def init_logger(file_path="log/ihrm.log", log_level=logging.DEBUG, when='midnight', interval=1, backupCount=3):
    """初始化日志工具"""
    if config.LOGGER is None:
        # 设置日志的输出格式
        log_format = "%(asctime)s|%(levelname)s|%(filename)s:%(funcName)s:%(lineno)d|-%(message)s"
        fmt = logging.Formatter(log_format)

        # 创建日志管理器
        file_handler = handlers.TimedRotatingFileHandler(file_path, when=when, interval=interval,
                                                         backupCount=backupCount, encoding="utf-8")
        stream_handler = logging.StreamHandler()

        # 日志管理器添加格式
        file_handler.setFormatter(fmt)
        stream_handler.setFormatter(fmt)

        # 创建日志器
        logger = logging.getLogger("test")
        logger.setLevel(log_level)
        logger.addHandler(file_handler)
        logger.addHandler(stream_handler)

        config.LOGGER = logger
    return config.LOGGER
